<?php

namespace App\Http\Controllers\Home\question;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Answer;
use App\User;
use App\Detail;
use App\Comments;

class PraiseController extends Controller
{
    public function showPraises(Request $request)
    {
        $id = $request->input('answer_id');
        $user_id = session('user_id');
        $comments = Answer::findOrFail($id)->getComments;
        $arr = [];
        $i = 0;
        foreach ($comments as $v) {
            $arr[$i]['content'] = $v->comments_content;
            $arr[$i]['pid'] = $v->pid;
            $arr[$i]['name'] = Detail::find($v->user_id)->nickname;
            $arr[$i]['portrait'] = Detail::find($v->user_id)->user_portrait;
            $arr[$i]['num'] = $v->getPraiseByComments->count();
            $arr[$i]['time'] = $v->update_time;
            $i++;
        }
        usort($arr, function($a, $b) {
            return $a['time'] - $b['time'];
        });
        $logo = $request->input('logo');
        if ($logo == 'add'){
            echo $comments->count().'条评论';
        }else {
        $str = '<div class="comment-app-holder" style="">
    <div data-reactid=".0" class="_CommentBox_root_G_1m _CommentBox_bordered_3Fo-" data-za-module="CommentList">
        <i data-reactid=".0.0" class="icon icon-spike" style="left: 142px;"></i>
        <div data-reactid=".0.4" class="_CommentBox_list_10_K">';
        foreach ($arr as $v) {
            $str .= '<div data-reactid=".0.4.$180232839" data-id="180232839" aria-label="王雪莽的评论" tabindex="-1" class="_CommentItem_root_PQNS" data-za-module="CommentItem">
    <a data-reactid=".0.4.$180232839.0" class="_CommentItem_avatarLink_3V51 hidden-phone" tabindex="-1" target="_blank" href="http://www.zhihu.com/people/wang-xue-mang" data-hovercard="p$b$wang-xue-mang">
        <img data-reactid=".0.4.$180232839.0.0" srcset="https://pic3.zhimg.com/6f49904f59466e07d30cc095152e45ee_xs.jpg 2x" src="/zhihu/home/'.$v['portrait'].'" class="Avatar Avatar-s" tabindex="-1"></a>
    <div data-reactid=".0.4.$180232839.1" class="_CommentItem_body_3qwB">
        <div data-reactid=".0.4.$180232839.1.0" class="_CommentItem_header_2JGh">
            <a data-reactid=".0.4.$180232839.1.0.0" target="_blank" href="http://www.zhihu.com/people/wang-xue-mang" data-hovercard="p$b$wang-xue-mang">'.$v['name'].'</a></div>
        <div data-reactid=".0.4.$180232839.1.1" class="_CommentItem_content_CYqW">'.$v['content'].'</div>
        <div data-reactid=".0.4.$180232839.1.2" class="_CommentItem_footer_46v8 clearfix">
            <span data-reactid=".0.4.$180232839.1.2.0" data-tooltip="s$r$171 人觉得这个很赞" class="_CommentItem_likes_2hey">
                <span data-reactid=".0.4.$180232839.1.2.0.0">'.$v['num'].'</span>
                <span data-reactid=".0.4.$180232839.1.2.0.1">赞</span></span>
            <time data-reactid=".0.4.$180232839.1.2.3" title="2016-11-08 15:39:01">1 天前</time>
            <button data-reactid=".0.4.$180232839.1.2.5" class="_CommentItem_action_Hk0w _CommentBox_textButton_3t9_">
                <i data-reactid=".0.4.$180232839.1.2.5.0" class="zg-icon zg-icon-comment-reply"></i>
                <span data-reactid=".0.4.$180232839.1.2.5.1">回复</span></button>
            <button data-reactid=".0.4.$180232839.1.2.7" class="_CommentItem_action_Hk0w _CommentBox_textButton_3t9_">
                <i data-reactid=".0.4.$180232839.1.2.7.0" class="zg-icon zg-icon-comment-like"></i>
                <span data-reactid=".0.4.$180232839.1.2.7.1">赞</span></button>
        </div>
    </div>
</div>';
        }
        $str .= '</div>
        <div hidden="" data-reactid=".0.6" class="_Pager_root_2GoR _CommentBox_pagerBorder_yuO1">
        </div>
        <div data-reactid=".0.7" class="_CommentForm_root_1-ko _CommentForm_bordered_2isg">
            <div contenteditable="true" data-reactid=".0.7.1" aria-label="写下你的评论…" data-input-box="true" class="_InputBox_root_1Xwi _InputBox_empty_3_ur _InputBox_blur_3JWV" placeholder="写下你的评论…" value="" required="">
            </div>
            <div data-reactid=".0.7.2" class="_CommentForm_actions_ooEq clearfix">
                <button data-reactid=".0.7.2.0" class="_CommentForm_submitButton_1N49 zg-btn-blue">评论</button>
                <button data-reactid=".0.7.2.1" data-key="cancel" class="_CommentForm_cancelButton_cy3u _CommentBox_textButton_3t9_">取消</button>
            </div>
        </div>
    </div>
</div>';
        echo $str;
    }

    }
}
